package com.yhao.stock.common.utils;

/**
 * @author : yhao
 * @Date : 2023/11/28 - 8:10
 * @Desc : TODO
 */

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelExporter {

    public static void export(List<List<Object>> data, String filePath) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        int rowNum = 0;
        for (List<Object> rowData : data) {
            Row row = sheet.createRow(rowNum++);

            int colNum = 0;
            for (Object cellData : rowData) {
                Cell cell = row.createCell(colNum++);

                if (cellData instanceof String) {
                    cell.setCellValue((String) cellData);
                } else if (cellData instanceof Integer) {
                    cell.setCellValue((Integer) cellData);
                } else if (cellData instanceof Double) {
                    cell.setCellValue((Double) cellData);
                }
            }
        }

        try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // public static void main(String[] args) {
    //     List<List<Object>> data = List.of(
    //             List.of("Name", "Age", "Score"),
    //             List.of("John Doe", 25, 85.5),
    //             List.of("Jane Smith", 30, 92.0)
    //     );
    //
    //     String filePath = "output.xlsx";
    //     export(data, filePath);
    //
    //     System.out.println("Excel exported successfully to " + filePath);
    // }
}